const http = require("http");
const server = http.createServer();
server.on("request", function (request, response) {
  console.log(request.url);
  console.log(request.socket.remoteAddress, request.socket.remotePort);
  response.setHeader("Content-type", "application/json; charset=utf-8");
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.setHeader("Access-Control-Allow-Headers", '*');
  response.setHeader("Access-Control-Allow-Method", '*');
  //write可以用来给客户端发送请求
  //write可以使用多次，但是最后一定要使用END来结束响应，否则客户端会一直等待
  response.write(JSON.stringify({
    errno: 0,
    data: [
      {user: '张三', content: '留言1'},
      {user: '李四', content: '留言2'}
    ]
  }));
  //response.end()支持两种数据类型，一种是二进制一种是字符串
  response.end();
});
server.listen(3000, function () {
  console.log("服务端启动成功，端口：3000");
});
